/**
 * Copyright (C), 2021, 杭州定川信息技术有限公司
 * FileName: UreportConfig
 * Author:   guo@tt
 * Date:     2021/3/4 22:17
 * Description: UreportConfig
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名         修改时间         版本号           描述
 */
package com.mt.roll.report.config;


import com.bstek.ureport.console.UReportServlet;
import com.bstek.ureport.definition.datasource.BuildinDatasource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.ImportResource;

import javax.annotation.Resource;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.SQLException;

/**
 * 〈// /ureport/* 不可更改 〉
 *  @ImportResource("classpath:ureport-console-context.xml")//不加项目能够启动但是会导致加载数据源报错或加载不了
 * @author guo@tt
 * @create 2021/3/4
 */
@ImportResource("classpath:ureport-console-context.xml")
@Configuration
@EnableAutoConfiguration
public class UreportConfig implements BuildinDatasource {

    @Resource
    DataSource dataSource;

    private Logger log = LoggerFactory.getLogger(getClass());

    /**
     *  定义ureport的启动servlet
     * @return
     */
    @Bean
    public ServletRegistrationBean buildUreportServlet(){
        return new ServletRegistrationBean(new UReportServlet(),"/ureport/*");
    }

    @Override
    public String name() {
        return "报表数据源";
    }

    @Override
    public Connection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            log.error("报表数据源获取连接失败！");
            e.printStackTrace();
        }
        return null;
    }

}